#NoEnv

;================================================================
;
; msgInfo
; msgErr
;
;================================================================

msgInfo(msg, title = "")
{
  TrayTip, %title%, %msg%,, 1
}

;----------------------------------------------------------------

msgErr(msg, title = "")
{
  TrayTip, %title%, %msg%,, 3
}

;================================================================
;
; envGet
;
;================================================================

envGet(name)
{
  EnvGet result, %name%
  return result
}

;================================================================
;
; launchProgram
;
;================================================================

launchProgram(title, exePath)
{
  SetTitleMatchMode, 2

  ifWinExist, %title%
  {
    WinActivate, %title%
    WinWaitActive, %title%
  }
  else
  {
    Run %exePath%,, UseErrorLevel, pid
    
    if ErrorLevel
    {
      msgErr("Cannot launch " . exe, "launchProgram")
      return false
    }
    
    Process, Priority, %pid%, High
    msgInfo("Launching " . title)
    
    WinWaitActive, %title%
    Process, priority, %pid%, Normal
    msgInfo("")
  }
  
  return true
}

;================================================================
;
; mountNetworkTools
;
;================================================================

mountNetworkTools()
{
  trueCrypt := envGet("Programs") . "\Utilities\TrueCrypt\TrueCrypt.exe"
;  Run %trueCrypt% /q background,, UseErrorLevel, pid

  IfNotExist, N:\
  {
    RunWait % trueCrypt . " /q /v " . envGet("Programs") . "\Net\NetTools.tc /lN"  . " /m timestamp",,,
    ; RunWait % trueCrypt . " /q /v " . """D:\Google Drive\NetTools.tc""" . " /lN" . " /m timestamp",,,
  }

  return true
}

;================================================================
;
; Programs
;
;================================================================

#f::
mountNetworkTools()
launchProgram("Firefox", "N:\Firefox\Program\firefox.exe -profile N:\Firefox\Data")
return

^!q::
mountNetworkTools()
EnvSet, PSIDATADIR, N:\Psi\Data
EnvSet, PSIPLUSDATADIR, N:\Psi\Data
launchProgram("Psi", "N:\Psi\Program\psi-plus.exe")
return


^F12::
launchProgram("Far", envGet("Programs") . "\Utilities\Far\far.exe")
WinRestore, "Far"
return

#n::
mountNetworkTools()
return

;================================================================
;
; Keyboard language switch
;
;================================================================

Capslock::SendInput, {Alt down}{Shift down}{Shift up}{Alt up}

;================================================================
;
; AutoHotKey
;
;================================================================

#F12::Suspend

#F5::Reload

#F4::
SetTitleMatchMode, 2

IfWinActive, Microsoft Visual Studio
{
  SendInput, ^o
  WinWaitActive, Open File
  SendInput, %A_ScriptFullPath%{Enter}
}
else
{
  launchProgram("edit AutoHotKey", envGet("Programs") . "\Utilities\Far\far.exe /e " . A_ScriptFullPath)
}
return

;================================================================
;
; Window management
;
;================================================================

#c::
   WinGet MX, MinMax, A
   if MX 
     WinRestore A
   else WinMaximize A
return

#x::WinClose A

^#x::
WinGet, active_pid, PID, A
Process, Close, %active_pid%
return

;================================================================
;
; Unmount
;
;================================================================

Unmount(name1, name2)
{
  EnvGet, Programs, Programs
  deveject = %Programs%\Utilities\deveject.exe

  IfNotExist, %deveject%
  {
    msgErr("Cannot find " . deveject, "Unmount")
    return false
  }

  RunWait, %deveject% "-EjectName:%name1%",, Hide UseErrorLevel

  if (ErrorLevel = 0)
  {
    msgInfo("Successfully unmounted " . name1, "Unmount")
    return true
  }
  
  if (ErrorLevel = 13)
  {
    RunWait, %deveject% "-EjectName:%name2%",, Hide UseErrorLevel
  }

  if (ErrorLevel = 0)
  {
    msgInfo("Successfully unmounted " . name2, "Unmount")
    return true
  }

  if ErrorLevel != 0
  {
    hint = Error code %ErrorLevel%
    if (ErrorLevel = 13) hint := "Cannot locate " . name1 . " or " . name2
    msgErr("DevEject: " . hint, "Unmount")
    return false
  }

  return true
}

;----------------------------------------------------------------

^!u::Unmount("SD Memory Card", "USB Mass Storage Device")

;================================================================
;
; Visual Studio
;
;================================================================

$#k::
SetTitleMatchMode, 2
IfWinNotActive, Microsoft Visual Studio
  SendInput, #k
else
  SendInput, !to {Left 3}{Home}Environment{Right}Keyboard{Tab 3}
return

$#t::
SetTitleMatchMode, 2
IfWinNotActive, Microsoft Visual Studio
  SendInput, #t
else
  SendInput, !to {Left 3}{Home}Text Editor{Right}C{Right}Tabs!t
return

$+3::
SetTitleMatchMode, 2
IfWinActive, Microsoft Visual Studio
  SendInput, ^!+3
else
  SendInput, +3
return

~!F7::
SetTitleMatchMode, 2
IfWinActive, Microsoft Visual Studio
{
  WinWaitActive, Property Pages
  SendInput +{Tab}{Left 5}{End}{Right}c
  SendInput, !cAll!p{End}{Tab 2}
}
 
return

;================================================================
;
; Mouse sensitivity
;
;================================================================

InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
; The caller must ensure that pDest has sufficient capacity.  To preserve any existing contents in pDest,
; only pSize number of bytes starting at pOffset are altered in it.
{
   Loop %pSize%  ; Copy each byte in the integer into the structure as raw binary data.
      DllCall("RtlFillMemory", "UInt", &pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF)
}

$#F7::
SPI_SETMOUSE = 0x0004
VarSetCapacity(MySet, 12, 0)
InsertInteger(0, MySet, 0)   ; MouseThreshold1
InsertInteger(0, MySet, 4)   ; MouseThreshold2
InsertInteger(0, MySet, 8)   ; MouseSpeed
DllCall("SystemParametersInfo", UInt, SPI_SETMOUSE, UInt, 0, Str, MySet, UInt, 1)

DllCall("SystemParametersInfo", Int, 113, Int, 0, UInt, 11, Int, 1)
msgInfo("Touchpad Mode")
return

$#F6::
SPI_SETMOUSE = 0x0004
VarSetCapacity(MySet, 12, 0)
InsertInteger(0, MySet, 0)   ; MouseThreshold1
InsertInteger(0, MySet, 4)   ; MouseThreshold2
InsertInteger(0, MySet, 8)   ; MouseSpeed
DllCall("SystemParametersInfo", UInt, SPI_SETMOUSE, UInt, 0, Str, MySet, UInt, 1)

DllCall("SystemParametersInfo", Int, 113, Int, 0, UInt, 8, Int, 1)
msgInfo("Mouse Mode")
return
